<?php

namespace App\Http\Controllers\Admin\Statistics;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\OtherOrder;
use App\Models\SystemUserLevel;
use App\Models\TransactionLog;
use App\Models\User;
use App\Models\UserBill;
use App\Models\UserRecharge;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Order\OrderTransformer;
use App\Transformers\Statistics\TransactionLogTransformer;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class TransactionLogController extends Controller
{
        use AdminTraits;
        public $model = TransactionLog::class;
        public $transformer = TransactionLogTransformer::class;


        public function finance(Request $request){

            $this->formatIndexQuery($request);
            $start = $request->input('start') ?? '2000-1-1';
            $end = $request->input('end') ?? '2222-1-1';

            $ids = Order::query()->pimp()
                ->pluck('id');

            //今天的时间
            $data = [
               'data' => Carbon::today()->toDateTimeString(),
                'data1' => Carbon::today()->addHours(24)
                    ->toDateTimeString(),
            ];

            //本月的时间
            $data1 = [
                'data' => Carbon::today()->subDays(14)
                    ->toDateTimeString(),

                'data1' => Carbon::today()->addDays(15)
                    ->toDateTimeString(),
            ];

            //商品支付金额、充值金额
            $pay_price = Order::query()->pimp()
                    ->whereBetween('created_at',[$start,$end])
                    ->where('paid',1)
                    ->sum('pay_price');

            $price = UserRecharge::query()->pimp()
                    ->whereBetween('created_at',[$start,$end])
                    ->where('is_paid',1)
                    ->sum('price');

            $orther = OtherOrder::query()->pimp()
                    ->whereBetween('created_at',[$start,$end])
                    ->where('paid',1)
                    ->sum('pay_price');

                //营业额
            $turnover = $pay_price + $price + $orther;

            //今日订单金额
            $pay_price1 = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data)
                ->whereBetween('created_at',[$start,$end])
                ->sum('pay_price');

            //今日订单数
            $order_num = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //今日支付人数
            $pay_num =  Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereIn('user_id',$ids)
                ->wherebetween('created_at',$data)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //本月订单数
            $month_order = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data1)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //本月支付人数
            $month_pay = Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereIn('user_id',$ids)
                ->wherebetween('created_at',$data1)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //商品支付金额
            $pay_price = Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->sum('pay_price');

            //充值金额
            $price = UserRecharge::query()
                ->pimp()
                ->where('is_paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->sum('price');

            //购买会员金额
            $money = SystemUserLevel::query()
                ->pimp()
                ->where('is_pay',1)
                ->whereBetween('created_at',[$start,$end])
                ->sum('money');

            //线下收银金额
            $order = Order::query()
                ->pimp()
                ->where('pay_type','offline')
                ->whereBetween('created_at',[$start,$end])
                ->sum('pay_price');

            //支出金额
            $number = UserBill::query()
                ->pimp()
                ->where('pm',0)
                ->whereBetween('created_at',[$start,$end])
                ->sum('number');

            //余额支付金额
            $balance = Order::query()
                ->pimp()
                ->where('pay_type','balance')
                ->whereBetween('created_at',[$start,$end])
                ->sum('pay_price');

            //支付佣金金额
            $brokerage_price = User::query()
                ->pimp()
                ->where('is_promoter',1)
                ->whereBetween('created_at',[$start,$end])
                ->sum('brokerage_price');

            //商品退款金额
            $refund_price = Order::query()
                ->pimp()
                ->where('refund_status','refunded')
                ->whereBetween('created_at',[$start,$end])
                ->sum('refund_price');

            //交易毛利金额
            $transaction = $turnover - $number;

            $count = [

                    'name1' => '今日订单金额',
                    'pay_price1' => $pay_price1,

                    'name2' => '今日订单数',
                    'order_num' => $order_num,

                    'name3' => '今日支付人数',
                    'pay_num' => $pay_num,

                    'name4' => '本月订单数',
                    'month_order' => $month_order,

                    'name5' => '本月支付人数',
                    'month_pay' => $month_pay,

                    'name6' => '营业额',
                    'turnover' => $turnover,

                    'name7' => '交易毛利金额',
                    'sum' => $transaction,

                    'name8' => '商品支付金额',
                    'pay_price' => $pay_price,

                    'name9' => '购买会员金额',
                    'money' => $money,

                    'name10' => '充值金额',
                    'price' => $price,

                    'name11' => '线下收银金额',
                    'order' => $order,

                    'name12' => '支出金额',
                    'number' => $number,

                    'name13' => '余额支付金额',
                    'balance' => $balance,

                    'name14' => '支付佣金金额',
                    'brokerage_price' => $brokerage_price,

                    'name15' => '商品退款金额',
                    'refund_price' => $refund_price,

            ];

            return $this->response->array($count);

        }
        public function growth(Request $request){
            //昨天
            $last_yesterday = [
                'data' => Carbon::yesterday()->toDateTimeString(),
                'data1' => Carbon::yesterday()->addHours(24)
                    ->toDateTimeString(),
            ];
            //今天
            $data = [
                'data' => Carbon::today()->toDateTimeString(),
                'data1' => Carbon::today()->addHours(24)
                    ->toDateTimeString(),
            ];

            $this->formatIndexQuery($request);
            $start = $request->input('start') ?? '2000-1-1';
            $end = $request->input('end') ?? '2222-1-1';

            //本月的时间
            $data1 = [
                'data' => Carbon::today()->subDays(14)->toDateTimeString(),
                'data1' => Carbon::today()->addDays(15)->toDateTimeString(),
            ];
            //上一个月
            $last_month = [
                'month' => Carbon::today()->subDays(53)->toDateTimeString(),
                'month1' => Carbon::today()->subDays(22)->toDateTimeString(),
            ];

            //今年的环比增长的时间
            $this_year = [
                'data' => Carbon::today()->subMonths(8)->subDays(21)
                    ->toDateTimeString(),
                'data1' => Carbon::today()->addMonths(3)->addDays(9)
                    ->toDateTimeString(),
            ];
            //上一年的环比增长的时间
            $last_year = [
                'data' => Carbon::today()->subYears()->subMonths(8)->subDays(21)
                    ->toDateTimeString(),
                'data1' => Carbon::today()->subYears()->addMonths(3)->addDays(9)
                    ->toDateTimeString(),
            ];
            //环比增长率
            //今日订单数
            $order_num = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //今日支付人数
            $pay_num =  Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data)
                ->whereBetween('created_at',[$start,$end])
                ->count();
            //本月订单数
            $month_order = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data1)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //本月支付人数
            $month_pay = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$data1)
                ->whereBetween('created_at',[$start,$end])
                ->count();
            //上一个月订单数
            $month_order1 = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$last_month)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //上一个月支付人数
            $month_pay1 = Order::query()
                ->pimp()
                ->where('paid',1)
                ->wherebetween('created_at',$last_month)
                ->whereBetween('created_at',[$start,$end])
                ->count();

            //昨天订单数
            $order_num1 = Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_yesterday)
                ->count();

            //昨天支付人数
            $pay_num1 =  Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_yesterday)
                ->count();

            //商品支付金额、充值金额
            $pay_price = Order::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->where('paid',1)
                ->sum('pay_price');

            $price = UserRecharge::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->where('is_paid',1)
                ->sum('price');

            $orther = OtherOrder::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->where('paid',1)
                ->sum('pay_price');

            //营业额
            $turnover = $pay_price + $price + $orther;

            //商品支付金额
            $pay_price = Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('pay_price');

            //充值金额
            $price = UserRecharge::query()
                ->pimp()
                ->where('is_paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('price');

            //购买会员金额
            $money = SystemUserLevel::query()
                ->pimp()
                ->where('is_pay',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('money');

            //线下收银金额
            $order = Order::query()
                ->pimp()
                ->where('pay_type','offline')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('pay_price');

            //支出金额
            $number = UserBill::query()
                ->pimp()
                ->where('pm',0)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('number');

            //余额支付金额
            $balance = Order::query()
                ->pimp()
                ->where('pay_type','balance')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('pay_price');

            //支付佣金金额
            $brokerage_price = User::query()
                ->pimp()
                ->where('is_promoter',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('brokerage_price');

            //商品退款金额
            $refund_price = Order::query()
                ->pimp()
                ->where('refund_status','refunded')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$this_year)
                ->sum('refund_price');

            //交易毛利金额
            $transaction = $turnover - $number;

            //上一年
            //商品支付金额、充值金额
            $pay_price1 = Order::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->where('paid',1)
                ->sum('pay_price');

            $price1 = UserRecharge::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->where('is_paid',1)
                ->sum('price');

            $orther1 = OtherOrder::query()->pimp()
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->where('paid',1)
                ->sum('pay_price');

            //营业额
            $turnover1 = $pay_price1 + $price1 + $orther1;

            //商品支付金额
            $pay_price1 = Order::query()
                ->pimp()
                ->where('paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('pay_price');

            //充值金额
            $price1 = UserRecharge::query()
                ->pimp()
                ->where('is_paid',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('price');

            //购买会员金额
            $money1 = SystemUserLevel::query()
                ->pimp()
                ->where('is_pay',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('money');

            //线下收银金额
            $order1 = Order::query()
                ->pimp()
                ->where('pay_type','offline')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('pay_price');

            //支出金额
            $number1 = UserBill::query()
                ->pimp()
                ->where('pm',0)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('number');

            //余额支付金额
            $balance1 = Order::query()
                ->pimp()
                ->where('pay_type','balance')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('pay_price');

            //支付佣金金额
            $brokerage_price1 = User::query()
                ->pimp()
                ->where('is_promoter',1)
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('brokerage_price');

            //商品退款金额
            $refund_price1 = Order::query()
                ->pimp()
                ->where('refund_status','refunded')
                ->whereBetween('created_at',[$start,$end])
                ->whereBetween('created_at',$last_year)
                ->sum('refund_price');

            //交易毛利金额
            $transaction1 = $turnover - $number;

            if($pay_num1 == 0){
                $pay_num2 = 0 .'%';
            }else{
                $pay_num2 = round(($pay_num - $pay_num1) / $pay_num1 * 100,2).'%';
            }

            if($order_num1 == 0){
                $order_num2 = 0 .'%';
            }else{
                $order_num2 = round(($order_num - $order_num1) / $order_num1 * 100,2).'%';
            }

            if($month_pay1 == 0){
                $month_pay2 = 0 .'%';
            }else{
                $month_pay2 = round(($month_pay - $month_pay1) / $month_pay1 * 100,2).'%';
            }

            if($month_order1 == 0){
                $month_order2 = 0 .'%';
            }else{
                 $month_order2 = round(($month_order - $month_order1) / $month_order1 * 100,2).'%';
            }

            if($turnover1 == 0){
                $turnover2 = 0 .'%';
            }else{
                $turnover2 = round(($turnover - $turnover1) / $turnover1 * 100,2).'%';
            }
            if($pay_price1 == 0){
                $pay_price2 = 0 .'%';
            }else{
                $pay_price2 = round(($pay_price - $pay_price1) / $pay_price1 * 100,2).'%';
            }

            if($price1 == 0){
                $price2 = 0 .'%';
            }else{
                $price2 = round(($price - $price1) / $price1 * 100,2).'%';

            }

            if($money1 == 0){
                $money2 = 0 .'%';
            }else{
                $money2 = round(($money - $money1) / $money1 * 100,2).'%';

            }

            if($order1 == 0){
                $order2 = 0 .'%';
            }else{
                $order2 = round(($order - $order1) / $order1 * 100,2).'%';

            }

            if($number1 == 0){
                $number2 = 0 .'%';
            }else{
                $number2 = round(($number - $number1) / $number1 * 100,2).'%';

            }

            if($balance1 == 0){
                $balance2 = 0 .'%';
            }else{
                $balance2 = round(($balance - $balance1) / $balance1 * 100,2).'%';

            }

            if($brokerage_price1 == 0){
                $brokerage_price2 = 0 .'%';
            }else{
                $brokerage_price2 = round(($brokerage_price - $brokerage_price1) / $brokerage_price1 * 100,2).'%';

            }

            if($refund_price1 == 0){
                $refund_price2 = 0 .'%';
            }else{
                $refund_price2 = round(($refund_price - $refund_price1) / $refund_price1 * 100,2).'%';

            }

            if($refund_price1 == 0){
                $transaction2 = 0 .'%';
            }else{
                $transaction2 = round(($transaction - $transaction1) / $transaction1 * 100,2).'%';

            }
            $count = [
                'order_num' => $order_num2,

                'pay_num' => $pay_num2 ,

                'month_order' => $month_order2,

                'month_pay' => $month_pay2,

                'turnover' => $turnover2,

                'sum' => $transaction2,

                'pay_price' => $pay_price2,

                'money' => $money2,

                'price' => $price2,

                'order' => $order2,

                'number' => $number2,

                'balance' => $balance2,

                'brokerage_price' => $brokerage_price2,
                
                'refund_price' => $refund_price2,
            ];

            return $this->response->array($count);
        }

        public function chart(){
            // 返回30天时间
            for ($i = 0; $i < 30; $i++) {
                $time[] = Carbon::now()->subDays($i)->toDateString();
            }

            //返回30天商品数据
            for ($i = 0; $i < 30; $i++) {
                $start_time = Carbon::now()->startOfDay()->subDays($i);
                $end_time = Carbon::now()->endOfDay()->subDays($i);
                $data[$i] = $this->getQuery()
                    ->whereBetween('created_at', [$start_time, $end_time])
                    ->selectRaw('SUM(turnover_price) as turnover_price,
                            SUM(transactior_price) as transactior_price,
                            SUM(pay_price) as pay_price,
                            SUM(money_price) as money_price,
                            SUM(price) as price,
                            SUM(number) as number'
                    )->get();
            }

            //改变数据格式
            foreach ($data as $k => $v){
                foreach ($v as $a => $b){
                    $arr[0]['data'][] = $b['turnover_price'];
                    $arr[1]['data'][] = $b['transactior_price'];
                    $arr[2]['data'][] = $b['pay_price'];
                    $arr[3]['data'][] = $b['money_price'];
                    $arr[4]['data'][] = $b['price'];
                    $arr[5]['data'][] = $b['number'];
                }
            }

            $arr[0]['name'] = '营业额';
            $arr[0]['type'] = 'line';
            $arr[1]['name'] = '交易毛利金额';
            $arr[1]['type'] = 'line';
            $arr[2]['name'] = '商品支付金额';
            $arr[2]['type'] = 'line';
            $arr[3]['name'] = '购买会员金额';
            $arr[3]['type'] = 'line';
            $arr[4]['name'] = '充值金额';
            $arr[4]['type'] = 'line';
            $arr[5]['name'] = '支出金额';
            $arr[5]['type'] = 'line';
            $array['series'] = $arr;
            $array['xAxis'] = $time;

            return $this->response->array($array);
        }

}
